const fsm = wx.getFileSystemManager();
const FILE_BASE_NAME = 'tmp_base64src';

// base64转image
function imageUrlWithBase64src(base64data) {
  return new Promise((resolve, reject) => {
    const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
    if (!format) {
      reject(new Error('ERROR_BASE64SRC_PARSE'))
    }
    const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME + Date.now()}.${format}`;
    const buffer = wx.base64ToArrayBuffer(bodyData);
    fsm.writeFile({
      filePath,
      data: buffer,
      encoding: 'binary',
      success: () => {
        resolve(filePath);
      },
      fail: () => {
        reject(new Error('ERROR_BASE64SRC_WRITE'))
      },
    });
  })
};

module.exports = {
  imageUrlWithBase64src
}